package com.sky.controller.admin;


import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/*
* 通用接口
* */
@Api(tags = "通用接口")
@RestController
@RequestMapping("/admin/common")
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){
        try {
            String originalFilename=file.getOriginalFilename();
            int index=originalFilename.lastIndexOf(".");
            String extname=originalFilename.substring (index);
            String objectName= UUID.randomUUID().toString()+extname;
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            //上传文件的二进制，上传文件的名称,名称建议uuid要手动
            //工具类里的方法会给你计算地址，并上传
            return Result.success(filePath);
        } catch (IOException e) {
            System.out.println("文件上传失败");
        }
        //文件上传失败
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }
}
